package com.fertshop.gui.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import com.fertshop.gui.Main;
import com.fertshop.gui.MainPanel;

public class PrintActionListener implements ActionListener{
	
	private MainPanel mainPanel;

	public PrintActionListener(MainPanel mainPanel){
		this.mainPanel=mainPanel;
	}
	
	
	
	@Override
	public void actionPerformed(ActionEvent event) {
		
		JInternalFrame printIf=new JInternalFrame("Print");
		JTextPane textPane=new JTextPane();
		JScrollPane scroll=new JScrollPane(textPane);
		printIf.setContentPane(scroll);
		StyledDocument doc = textPane.getStyledDocument ();
		textPane.setLogicalStyle (doc.getStyle ("bold"));
		Style def = StyleContext.getDefaultStyleContext().getStyle(StyleContext.DEFAULT_STYLE);

		Style regular = doc.addStyle("regular", def);
		StyleConstants.setFontFamily(def, "Times New Roman");
		StyleConstants.setFontSize(def, 12) ;
		Style s = doc.addStyle("bold", regular);
		StyleConstants.setBold(s, true);
		s = doc.addStyle("underline", regular);
		StyleConstants.setUnderline(s, true) ;
		StyleConstants.setBold(s, true);
		s = doc.addStyle("fourteen", regular);
		StyleConstants.setBold(s, true);
		StyleConstants.setFontSize(s, 14) ;
		try{
			
            doc.insertString (doc.getLength(),
                    "\n\n\n\n\n\n\n\n\n\n\n\n\n                                           " +
                    "                                          RENEWAL",
                    doc.getStyle ("bold"));
            doc.insertString (doc.getLength(),
                    "\n                                                                 " +
                    "                    License No : "+mainPanel.getLicenseNoTf().getText(),
                    doc.getStyle ("bold"));
            doc.insertString (doc.getLength(),
                    "\n                                                                 " +
                    "                    Valid up to  : ",
                    doc.getStyle ("bold"));
            doc.insertString (doc.getLength(),
                    "\n\n1. M/s. "+mainPanel.getShopNameTf().getText()+", SOWPADU[V], VATTICHERUKURU[M] ",
                    doc.getStyle ("fourteen"));
            doc.insertString (doc.getLength(),
                    "are hereby licensed to sell. Stock Exhibit for sale or Distribute by retail, " +
                    "insecticides as furnished below on the premises situated at: ",
                    doc.getStyle ("regular"));
            doc.insertString (doc.getLength(),
                    "\n\nSales  :  ",
                    doc.getStyle ("bold"));
            doc.insertString (doc.getLength(),
                    "Door.No:  "+mainPanel.getDoorNoTf().getText()+", Main Road, "+mainPanel.getAreaTf().getText()+".",
                    doc.getStyle ("regular"));
            doc.insertString (doc.getLength(),
                    "\n\nStorage:  ",
                    doc.getStyle ("bold"));
            doc.insertString (doc.getLength(),
                    "Door.No:  54, Main Road, Sowpadu.\n\n\nSubject to the conditions specified " +
                    "overleaf and to the provisions of the insecticides Act. 1968 and the " +
                    "rules there under.",
                    doc.getStyle ("regular"));
            doc.insertString (doc.getLength(),
                    "\n2. ",
                    doc.getStyle ("bold"));
            doc.insertString (doc.getLength(),
                    "License shall be in force from ",
                    doc.getStyle ("regular"));
            doc.insertString (doc.getLength(),
                    "01-01-2012 ",
                    doc.getStyle ("underline"));
            doc.insertString (doc.getLength(),
                    "to ",
                    doc.getStyle ("regular"));
            doc.insertString (doc.getLength(),
                    "31-12-2013. ",
                    doc.getStyle ("underline"));
            doc.insertString (doc.getLength(),
                    "\n\n3. ",
                    doc.getStyle ("bold"));
            doc.insertString (doc.getLength(),
                    "Categories of insecticides supplied by the Principals as detailed below. ",
                    doc.getStyle ("regular"));
            
        } catch (BadLocationException ble) {
            ble.printStackTrace ();
        }
		JMenuBar menuBar=new JMenuBar();
		JMenu printMenu=new JMenu("Print");
		printMenu.addMenuListener(new PrintMenuListener(textPane));
		menuBar.add(printMenu);
		
		 printIf.setJMenuBar(menuBar);
		Main.desktop.add(printIf);
		printIf.setClosable(true);
		printIf.setMaximizable(true);
		printIf.setResizable(true);
		printIf.setIconifiable(true); 
		try { 
			printIf.setSelected(true); 
	 	} catch (java.beans.PropertyVetoException e2) { 
	 	  } 
		printIf.setSize(500, 500);
		printIf.setVisible(true);
		
	}

}
